<template>
  <div class="list-content">
    <el-form :inline="true" :model="formInline" class="demo-form-inline" @submit.native.prevent>
      <el-form-item v-if="isMuilUnitUser">
        <el-select filterable @change="getData(1)" v-model="formInline.orgId" placeholder="请选择业主单位">
          <el-option
            v-for="item in orgOptions"
            :key="item.organizationId"
            :label="item.shortName"
            :value="item.organizationId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input 
        v-model.trim="formInline.name" 
        placeholder="请输入报告名称"
        @keyup.enter.native="getData(1)"
        clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button class="form-search-btn" type="primary" @click="getData(1)">查询</el-button>
      </el-form-item>
      <div style="float:right;" v-if="isMuilUnitUser">
        <el-popover
          placement="left"
          width="250"
          v-model="visible">
          <div v-loading="reportLoading">
            <el-select filterable clearable v-model="orgId" placeholder="请选择业主单位">
              <el-option
                v-for="item in orgList"
                :key="item.organizationId"
                :label="item.shortName"
                :value="item.organizationId">
              </el-option>
            </el-select>
            <div style="text-align: right; margin: 10px 0 0;">
              <el-button size="mini" @click="visible = false">取消</el-button>
              <el-button type="primary" size="mini" :disabled="!orgId" @click="createReport">确定</el-button>
            </div>
          </div>
          <el-button slot="reference" type="primary">手动生成本月维保报告</el-button>
        </el-popover>
      </div>
      <div style="float:right;" v-else>
        <el-button type="primary" :loading="reportLoading" @click="createReport">手动生成本月维保报告</el-button>
      </div>
    </el-form>
    <el-row class="overclick-wrap" :gutter="40" type="flex" justify="space-between">
      <span style="display:none;">{{freashStaticFlag}}</span>
      <el-col :span="8">
        <div class="item-wrap" @click="formInline.status=''" :class="formInline.status=='' ? 'active' : ''">
          <div class="item-content">
            <p class="item-value color-1">{{ staticObj.totalNum }}</p>
            <p class="item-title">报告总数（个）</p>
          </div>
        </div>
      </el-col>
      <el-col :span="8">
        <div class="item-wrap" @click="formInline.status='1'" :class="formInline.status==1 ? 'active' : ''">
          <div class="item-content">
            <p class="item-value color-2">{{ staticObj.unSubmitNum}}</p>
            <p class="item-title">未提交</p>
          </div>
        </div>
      </el-col>
      <el-col :span="8">
        <div class="item-wrap" @click="formInline.status='2'" :class="formInline.status==2 ? 'active' : ''">
          <div class="item-content">
            <p class="item-value color-4">{{ staticObj.submitNum }}</p>
            <p class="item-title">已提交</p>
          </div>
        </div>
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="tableData"
      ref="mainTable"
      style="width: 100%">
      <el-table-column
      label="序号"
      type="index"
      width="50">
      </el-table-column>
      <el-table-column
        v-if="isMuilUnitUser"
        prop="orgName"
        label="业主单位">
      </el-table-column>
      <el-table-column
      prop="pronum"
      label="报告编号">
      </el-table-column>
      <el-table-column
      prop="reportName"
      label="报告名称">
        <template slot-scope="scope">
          <el-link type="primary" @click="openReport(scope.row)">{{scope.row.reportName}}</el-link>
        </template>
      </el-table-column>
      <el-table-column
        prop="period"
        label="报告周期"
        width="100px">
        <template slot-scope="scope">
          <p v-if="scope.row.period == 1">周报</p>
          <p v-if="scope.row.period == 2">月报</p>
        </template>
      </el-table-column>
      <el-table-column
        label="报告期" width="360px">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.ranges }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="submitter"
        label="提交人">
      </el-table-column>
      <el-table-column
        prop="submitTime"
        label="提交时间">
      </el-table-column>
      <el-table-column prop="status" label="状态" width="100px">
        <template  slot-scope="scope">     
          <span :class="'tatus-icon-tags ' + (scope.row.status == 2 ? 'bg-color-4' : 'bg-color-2')"></span>{{scope.row.status == 2 ? '已提交' : '未提交'}}
        </template>  
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="sizeChange"
      @current-change="pageChange"
      :current-page="formInline.currentPageIndex"
      :page-sizes="[10, 50, 100]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="formInline.currentPageTotal">
    </el-pagination>
    <!-- 报告详情弹框 -->
    <el-dialog
      :close-on-click-modal="false"
      title="维保报告"
      width="1000px"
      top='5vh'
      v-if="dialogReport"
      :visible.sync="dialogReport"
      @close="closeReportDialog"
      v-dialogDrag>
      <report-print :reportId="reportId" :reportNum="reportNum" :status="reportStatus" :closeFun="closeReportDialog"></report-print>
    </el-dialog>
  </div>
</template>

<script>
import { getOrgList } from '@/api/org'
import util from '@/utils/util'
import ReportPrint from './ReportPrint/Main'
import { getReportList, state, createReport } from '@/api/maintenanceReport'

export default {
  name: 'MaintenReport',
  components: { 
    ReportPrint
  },
  props:['param'],
  data () {
    return {
      freashStaticFlag:0,
      formInline: {
        orgId: this.$store.state.userinfo.isMuilUnitUser ? this.$store.state.userinfo.organizationIds : this.$store.state.userinfo.org.id,
        name: '',
        status: '',
        currentPageSize: 10,
        currentPageIndex: 1,
        currentPageTotal: 0
      },
      util:util,
      isMuilUnitUser: this.$store.state.userinfo.isMuilUnitUser,
      tableData: [], // 表格数据
      staticObj: {}, // 头部各状态个数统计
      orgOptions: [{organizationId: this.$store.state.userinfo.organizationIds,shortName: "全部业主单位"}],
      loading: false,
      // 维保报告
      dialogReport: false,
      reportId: '',
      reportNum: '',
      reportStatus: '',
      orgId:'',
      visible:false,
      reportLoading:false,
      orgList:[]
    }
  },
  created:function() {
    //绑定下拉框
    if(this.isMuilUnitUser){
      getOrgList({queryType: 0}).then((data)=>{
        this.orgOptions.push.apply(this.orgOptions,data.data);
        this.orgList=data.data
      })
    }
    this.getData()
  },
  methods: {
    getData(_pageIndex){
      this.getList(_pageIndex)    
      this.getStatis() 
    },
    sizeChange(val) {//页码切换
      this.formInline.currentPageIndex = 1;
      this.formInline.currentPageSize = val;
      this.getList() 
    },
    pageChange(val) {//翻页
      this.formInline.currentPageIndex = val;
      this.getList() 
    },
    getList(_pageIndex) {//获取列表
      if(_pageIndex){
        this.formInline.currentPageIndex = _pageIndex
      }
      this.loading =true
      getReportList({
        orgIds: this.formInline.orgId,
        name: this.formInline.name.trim(),
        status: this.formInline.status,
        reportType: 1,
        pageIndex: this.formInline.currentPageIndex -1,
        pageSize: this.formInline.currentPageSize
      }).then((result) => {
        this.formInline.currentPageTotal = result.total;
        this.tableData = result.data;
        this.loading =false;
      }) 
    },
    getStatis() {//头部统计
      state({
        orgIds: this.formInline.orgId,
        name: this.formInline.name.trim()
      }).then((result) => {
          $.extend(this.staticObj,result.data) 
          this.freashStaticFlag++
      })
    },
    openReport(row){
      let that = this
      //TODO  打开报告弹窗
      that.reportId = row.id
      that.reportNum = row.pronum
      that.reportStatus = row.status
      that.dialogReport = true
    },
    closeReportDialog(flag) {
      let that = this
      that.dialogReport = false
      if(typeof(flag) !== 'object' && flag) {
        this.getData(1)
      }
    },
    createReport(){
      this.reportLoading =true
      createReport({
        orgId: this.isMuilUnitUser?this.orgId:this.formInline.orgId
      }).then((res) => {
        if(res.status==200){
          this.getData(1)
          this.$message.success(res.info)
          this.visible=false
        }else{
          this.$message.error(res.info)
        }
        this.reportLoading =false;
      }).catch(()=>{
        this.reportLoading =false;
      })
    }
  },
  watch: {
    handlerStatusSelect(val, oldVal) {
      if(val !== oldVal) {
        this.getList(1)
      }
    },
  },
  computed: { 
    handlerStatusSelect() {
      return this.formInline.status
    }
  }
}
</script>